package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.lang.Console;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    private Logger logger=LoggerFactory.getLogger(AdminController.class);



    @RequestMapping("/login")
    @LogAnnotation(value = "登录",type = "login")
    public String login(Admin admin,String code, HttpSession session){
        System.out.println(admin.getUserName()+""+admin.getPassword());
        System.out.println(admin);

        //1.数据封装token
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUserName(), admin.getPassword());
        //adminService.selectByUserName(admin.getUserName());
        //2.获取Subject
        Subject subject = SecurityUtils.getSubject();
     //获取验证码
        LineCaptcha captcha= (LineCaptcha) session.getAttribute("captcha");
        //3.调用登录方法
        if(captcha.verify(code.toLowerCase())){
            try{

                System.out.println("身份认证中");
                subject.login(token);

                System.out.println("认证成功");
                //演示授权代码
               /* boolean permitted = subject.isPermitted("banner:create");
                if(permitted){
                    System.out.println(permitted);
                }*/


                return "redirect:/main.jsp";
            }catch(Exception e){
                System.out.println("身份认证失败");

                return  "redirect:/login.jsp";
            }
        }else{
            System.out.println("身份认证失败");
            return  "redirect:/login.jsp";
        }





   /* @RequestMapping("/login")
    @LogAnnotation(value = "登录",type = "login")
    public String login(Admin admin,String code, HttpSession session){
        System.out.println(admin);
        LineCaptcha captcha = (LineCaptcha) session.getAttribute("captcha");
        if(captcha.verify(code.toLowerCase())) {
            Admin a = adminService.login(admin.getUserName(), admin.getPassword());
            if (a != null) {
                session.setAttribute("a",a);
                session.setAttribute("b",a.getUserName());
                return "main";
            }else{
                return "login";
            }
        }else{
            return "login";
        }


    }*/
   //身份认证
   /*@RequestMapping("/login")
   @LogAnnotation(value = "登录",type = "login")
   public String login(Admin admin,String code, HttpSession session){
       System.out.println(admin.getUserName()+""+admin.getPassword());
       System.out.println(admin);
       
       //1.数据封装token
       UsernamePasswordToken token = new UsernamePasswordToken(admin.getUserName(), admin.getPassword());
      //adminService.selectByUserName(admin.getUserName());
       //2.获取Subject
       Subject subject = SecurityUtils.getSubject();
//       ShearCaptcha captcha = (ShearCaptcha) session.getAttribute("");
       LineCaptcha captcha= (LineCaptcha) session.getAttribute("captcha");
       //3.调用登录方法
      if(captcha.verify(code.toLowerCase())){
          try{
              subject.login(token);

              return "redirect:/main.jsp";
          }catch(Exception e){
              return  "redirect:/login.jsp";
          }
      }else{
          return  "redirect:/login.jsp";
      }
*/


       /*ineCaptcha captcha = (LineCaptcha) session.getAttribute("captcha");
       if(captcha.verify(code.toLowerCase())) {
           Admin a = adminService.login(userName ,password);
           if (a != null) {
               session.setAttribute("a",a);
               session.setAttribute("b",a.getUserName());
               return "main";
           }else{
               return "login";
           }
       }else{
           return "login";
       }*/


   }
    //验证码
    @RequestMapping("getCaptcha")
    public  void getCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        //输出code
        Console.log(lineCaptcha.getCode());

        session.setAttribute("captcha",lineCaptcha);
    }

    @RequestMapping("/selectAllAdmin")
    @ResponseBody
    public Map selectAllAdmin(int page, int limit){
        return adminService.selectAllAdmin(page,limit);
    }


    @RequestMapping("/insertAdmin")
    @ResponseBody
    public Map insertAdmin(Admin admin){
        return adminService.insertAdmin(admin);
    }

    @RequestMapping("/updataAdmin")
    @ResponseBody
    public Map  updataAdmin(Admin admin){
        return adminService.updataAdmin(admin);
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public Admin selectById(int id){
        return adminService.selectById(id);
    }

    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public Map deleteAdmin(int id){
        return  adminService.deleteAdmin(id);
    }

}
